﻿/**
 *  Tools for loading in Cellular Automatons.
 *  Author: Anders Høst | ahkj@itu.dk
 *  Date: 11 dec 2011
 */
using System;
using System.Diagnostics.Contracts;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using AutomaTones.Model.Automaton_Model;
using AutomaTones.Model.Utils;

namespace AutomaTones.Model.Tools {
    public static class LoadCA {
        /// <summary>
        /// Intends to load a CellularAutomaton object for the path passed in. If path doesn't exist or point to 
        /// a file containing a serialized Cellular Automaton, the method returns null.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static CaSaveContainer Load(String path) {
           Contract.Requires(path.Length > 0);
            Stream s;
            try {
                s = File.Open(path, FileMode.Open);
            } catch (FileNotFoundException e){
                Console.WriteLine("Exception" + e.ToString());
                return null; 
            }
           BinaryFormatter bf = new BinaryFormatter();
           CaSaveContainer _ca = (CaSaveContainer)bf.Deserialize(s);
           s.Close();

           return _ca;
        }
    }
}
